//
// Copyright (c) 2024 BPer
//
#include <bperrtp/BPerRtcp.h>
#include <bperutil/sys/BPerLog.h>
#include <bperutil/sys/BPerEndian.h>

int32_t bPer_decode_rtcpHeader(BPerRtcpHeader* header,char* buf){
	if(header==NULL||buf==NULL) return 1;
	uint8_t first=(uint8_t)buf[0];
	header->rc=first&0x1F;
	header->padding=first&0x20;
	header->version=(first>>6)&0x3;
	header->type=(uint8_t)buf[1];
	header->length=bPer_get_be16((uint8_t*)buf+2);
	return BPer_Ok;
}

int32_t bPer_encode_rtcpHeader(BPerRtcpHeader* header,BPerBuffer* buffer){
	if(header==NULL||buffer==NULL) return 1;
	uint8_t first=header->version<<6|header->padding<<5|header->rc;
	bPer_write_1bytes(buffer,first);
	bPer_write_1bytes(buffer,header->type);
	bPer_write_2bytes(buffer,header->length);
	return BPer_Ok;
}
